package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.query.TravelObject;
import cn.wolfcode.trip.base.query.UserFansQuery;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.service.IUserService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import jdk.nashorn.internal.objects.annotations.Getter;
import org.aspectj.weaver.ast.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("users")
public class UserController {
    @Autowired
    private IUserService userService;
    @Autowired
    private ITravelService travelService;
  @PostMapping
    public JSONResult register(User user){
        JSONResult jsonResult=new JSONResult();
        try {
            userService.saveOrCheckEmail(user);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.mark(e.getMessage());
        }
        return jsonResult;
    }
  @PutMapping("/{id}")
    public JSONResult update(User user){
      userService.update(user);
      return new JSONResult().setResult(user);
  }

    @GetMapping("/{authorId}/travels")
    public PageInfo listQuery(TravelObject qo){
        qo.setOrderBy("t.lastUpdateTime desc");
        return travelService.query(qo);
    }

    @GetMapping("/{authorId}")
    public User getUserByAuthorId(@PathVariable Long authorId){

        return userService.getUserByAuthorId(authorId);
    }

    @PutMapping("/{id}/fans")
    public JSONResult updateFans(User user){
          JSONResult result=new JSONResult();
        try {
            userService.updateFans(user);
        } catch (Exception e) {
            result.mark(e.getMessage());
            return result;
        }
        return result;
    }
    @DeleteMapping("/{id}/fans")
    public JSONResult deleteFans(@PathVariable Long id){
        userService.deleteFans(id);
        return new JSONResult();
    }
    @GetMapping("/{id}/fans")
    public Map queryFans(@PathVariable Long id){
        return userService.queryFans(id);
    }
    @PostMapping("/{id}/like")
    public JSONResult like(Travel travel){
        JSONResult result = new JSONResult();
        try {
            userService.saveLike(travel);
        } catch (Exception e) {
            result.mark(e.getMessage());
        }
        return result;
    }
    @GetMapping("/{id}/like")
    public Map queryLike(@PathVariable Long id){
        HashMap map = userService.queryLike(id);
        System.out.println(map);
        return map;
    }

  @DeleteMapping("/{travelId}/like")
    public void deleteLike(@PathVariable Long travelId){
      userService.deleteLike(travelId);
  }

  @GetMapping
    public List<User> listAllFansOrFollow(UserFansQuery qo){
      List<User> users= userService.listAllFansOrFollow(qo);
      return users;
  }
}
